package com.mylims.ifdc.common.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class SpringContextHolder implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static <T> T getBean(Class<T> beanClass) {
        if (context == null) {
            throw new IllegalStateException("ApplicationContext not initialized");
        }
        return context.getBean(beanClass);
    }

    public static <T> T getBean(String beanName, Class<T> type) {
//        String[] beanNamesForType = context.getBeanNamesForType(SimpMessagingTemplate.class);
//        System.out.println(beanNamesForType);
        return context.getBean(beanName, type);
    }
}
